package lab.gene.dao;

import java.util.List;
import lab.gene.pojo.Gene;
import lab.gene.pojo.GeneImageFile;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

@SuppressWarnings("deprecation")
public class GeneImageFileDAO {
	
	private static SessionFactory factory = new AnnotationConfiguration().configure().addAnnotatedClass(GeneImageFile.class).addPackage("com.xyz").buildSessionFactory();

	public void addGeneImageFile(GeneImageFile g) {
		try {
	         Session session = factory.openSession();
	         Transaction tx = null;
	         try {
	        	 tx = session.beginTransaction();
	        	 session.save(g); 
	        	 tx.commit();
	        	 } 
	         catch (HibernateException e) {
	        	 if (tx!=null) 
	        		 tx.rollback();
	        	 e.printStackTrace(); 
	         } 
	         finally {
	            session.close(); 
	         }
	      } catch (Throwable ex) { 
	         System.err.println("Failed to create sessionFactory object." + ex);
	         throw new ExceptionInInitializerError(ex); 
	      }
		
	}
	
	@SuppressWarnings("unchecked")
	public List<GeneImageFile> getGeneImageFilesByGeneNameAndFormName(Gene gene, String formName) {
		List<GeneImageFile> imageList = null;
		try {
			Session session = factory.openSession();
	        try {
	        	Query query1 = session.createQuery("select ff from GeneImageFile as ff where ff.formName = '"+formName+"' and ff.gene=:gene and ff.fileName not in (select f.fileName from GeneImageFile as f where f.optMode = 'Del' and f.formName = '"+formName+"' and f.gene=:gene)").setParameter("gene", gene);
	        	List<GeneImageFile> results1 = query1.list();
	        	System.out.println("# geneImageFiles:"+results1.size());
	        	if (results1.size() > 0) {
		        	imageList = results1;
	        		System.out.println("files found!"); 
	        	}
	        	else {
	        		System.out.println("result.size = "+ results1.size());
	        		System.out.println("empty fileList");
	        	}
	        }
	        catch (HibernateException e) {
	        	e.printStackTrace();
	        } 
	        finally {
	            session.close(); 
	        }
	   } 
		catch (Throwable ex) { 
			System.err.println("Failed to create sessionFactory object." + ex);
	        throw new ExceptionInInitializerError(ex);
	   }
		return imageList;
	}
	
	@SuppressWarnings("unchecked")
	public List<GeneImageFile> getAllGeneImageFilesByGeneName(Gene gene) {
		List<GeneImageFile> imageList = null;
		try {
			Session session = factory.openSession();
	        try {
	        	Query query1 = session.createQuery("select ff from GeneImageFile as ff where ff.gene=:gene").setParameter("gene", gene);
	        	List<GeneImageFile> results1 = query1.list();
	        	System.out.println("# geneImageFiles:"+results1.size());
	        	if (results1.size() > 0) {
		        	imageList = results1;
	        		System.out.println("files found!"); 
	        	}
	        	else {
	        		System.out.println("result.size = "+ results1.size());
	        		System.out.println("empty fileList");
	        	}
	        }
	        catch (HibernateException e) {
	        	e.printStackTrace();
	        } 
	        finally {
	            session.close(); 
	        }
	   } 
		catch (Throwable ex) { 
			System.err.println("Failed to create sessionFactory object." + ex);
	        throw new ExceptionInInitializerError(ex);
	   }
		return imageList;
	}
	
	@SuppressWarnings("unchecked")
	public List<GeneImageFile> getGeneImageFilesByFileName(String fileName) {
		List<GeneImageFile> imageList = null;
		try {
			Session session = factory.openSession();
	        try {
	        	String hql = "FROM GeneImageFile P where P.fileName = '"+fileName+"'";
	        	System.out.println(hql);
	        	Query query = session.createQuery(hql);
	        	List<GeneImageFile> results = query.list();
	        	System.out.println("# geneImageFiles:"+results.size());
	        	if (results.size() > 0) {
	        		imageList = results;
	        		System.out.println("files found!"); 
	        	}
	        	else {
	        		System.out.println("result.size = "+ results.size());
	        		System.out.println("empty fileList");
	        	}
	        }
	        catch (HibernateException e) {
	        	e.printStackTrace();
	        } 
	        finally {
	            session.close(); 
	        }
	   } 
		catch (Throwable ex) { 
			System.err.println("Failed to create sessionFactory object." + ex);
	        throw new ExceptionInInitializerError(ex);
	   }
		return imageList;
	}
	
	@SuppressWarnings("unchecked")
	public GeneImageFile getGeneImageFilesById(int id) {
		GeneImageFile imageList = null;
		try {
			Session session = factory.openSession();
	        try {
	        	
	        	String hql = "FROM GeneImageFile P where P.id = '"+Integer.toString(id)+"'";
	        	System.out.println(hql);
	        	Query query = session.createQuery(hql);
	        	List<GeneImageFile> results = query.list();
	        	System.out.println("# geneImageFiles:"+results.size());
	        	if (results.size() > 0) {
	        		imageList = results.get(0);
	        		System.out.println("files found!"); 
	        	}
	        	else {
	        		System.out.println("result.size = "+ results.size());
	        		System.out.println("empty fileList");
	        	}
	        }
	        catch (HibernateException e) {
	        	e.printStackTrace();
	        } 
	        finally {
	            session.close(); 
	        }
	   } 
		catch (Throwable ex) { 
			System.err.println("Failed to create sessionFactory object." + ex);
	        throw new ExceptionInInitializerError(ex);
	   }
		return imageList;
	}
	
	public void deleteGeneImagebyId(int id) {
		
		Session session = factory.openSession();
		Transaction tx = null;
		try {
		   try {
			   tx = session.beginTransaction();
	        	String hql = "DELETE FROM GeneImageFile G "  + "WHERE G.id = '" + String.valueOf(id) + "'";
	        	System.out.println(hql);
	        	Query query = session.createQuery(hql);
	        	query.executeUpdate();
	        	tx.commit();
	        }
	        catch (HibernateException e) {
	        	e.printStackTrace();
	        } 
		}
		catch (Exception e) {
		   if (tx!=null) tx.rollback();
		   e.printStackTrace(); 
		}finally {
		   session.close();
		}
	}
}
